package cn.gzsxt.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.gzsxt.dao.TaskCheckDao;
import cn.gzsxt.dao.impl.TaskCheckDaoImpl;
import cn.gzsxt.po.PageCheckResult_name;
import cn.gzsxt.po.T_sysUser;
import cn.gzsxt.po.T_task;
import cn.gzsxt.po.Uname;
import cn.gzsxt.query.PageQueryObject;
import cn.gzsxt.service.TaskCheckService;

public class TaskCheckServiceImpl implements TaskCheckService {

	private TaskCheckDao dao = new TaskCheckDaoImpl();

	@Override
	public PageCheckResult_name task(PageQueryObject qo) {

		PageCheckResult_name pr = null;
		Integer totalCount = dao.checkTotalCountByCondition(qo);
		if (totalCount == null) {
			pr = new PageCheckResult_name(Collections.emptyList(), totalCount, qo.getPageSize(), qo.getCurrentPage());

		} else {

			int tp = totalCount % qo.getPageSize() == 0 ? totalCount / qo.getPageSize()
					: totalCount / qo.getPageSize() + 1;

			if (tp < qo.getCurrentPage()) {
				qo.setCurrentPage(tp);
			}

			List<T_task> list = (List<T_task>) dao.checkTask(qo);
			Map temp = new HashMap();
			for (T_task t_taskadd : list) {
				temp.put(t_taskadd.getPerson(), 1);
			}
			Set temp2 = temp.keySet();
			Object[] temp3 = temp2.toArray();
			List<Uname> nameMap = dao.getName(temp3);
			System.out.println(nameMap);
			pr = new PageCheckResult_name(list, totalCount, qo.getPageSize(), qo.getCurrentPage());
			pr.setUname(nameMap);
		}

		return pr;
	}

	@Override
	public Integer deltask(String id) {
		return dao.delTaskById(id);
	}

	@Override
	public int updateTask(T_task task, Integer... id) {
		return dao.updateTaskById(task, id);
	}

	@Override
	public List<T_sysUser> checkId_Name() {
		return dao.checkUserINameId();
	}

	@Override
	public List<T_task> checkById(String id) {
		return dao.checkTaskById(id);
	}

	@Override
	public Integer delpagetask(String[] ids) {
		return dao.delPageTaskById(ids);
	}

}
